//
//  main.m
//  05-循环遍历
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // 1> 创建数组
        // 2> 创建可变数组
        NSArray *stringArr = @[@"王尼玛", @"老司机",@"于涵", @"臭傻逼", @"国宝是最傻逼"];
        NSMutableArray *newArr = [NSMutableArray array];
        for (NSInteger i = stringArr.count - 1; i >= 0; i--) {
            [newArr addObject:stringArr[i]];
        }
        NSLog(@"%@",newArr);
        NSDictionary *dic = @{@"name":@"王尼玛", @"sex":@"男", @"age":@"25"};
        for (NSInteger i = 0; i < dic.allKeys.count; i++) {
            NSLog(@"%@ %@",dic.allKeys[i],[dic objectForKey:dic.allKeys[i]]);
        }
        // 枚举器
        // 依赖于集合类型包括数组字典集合,不能单独使用
        NSEnumerator *stringArrayEnumerator = [stringArr objectEnumerator];
        NSString *value = nil;
        while (value = [stringArrayEnumerator nextObject]) {
            NSLog(@"value = %@",value);
        }
        // 倒序
        NSEnumerator *rever = [stringArr reverseObjectEnumerator];
        while (value = [rever nextObject]) {
            NSLog(@"value = %@",value);
        }
        // 字典
        // 默认取出来的值,是value
        NSEnumerator *dicValueEnumerator = [dic objectEnumerator];
        while (value = [dicValueEnumerator nextObject]) {
            NSLog(@"%@",value);
            
        }
        // 修改成dic.allKeys 将字典变成keys的数组,打印出来的对象们就是key
        NSString *key = nil;
        NSEnumerator *dicKeysEnumerator = [dic.allKeys objectEnumerator];
        while (key = [dicKeysEnumerator nextObject]) {
            NSLog(@"%@",key);
        }
        // 集合枚举
        NSSet *set = [NSSet setWithObjects:@"110", @"111", @"112", nil];
        NSEnumerator *setEnumerator = [set objectEnumerator];
        while (value = [setEnumerator nextObject]) {
            NSLog(@"value = %@",value);
        }
        // 快速枚举 for...in (倒序只要在in 后面加上reverObjectEnumertor)
        for (NSString *string in [stringArr reverseObjectEnumerator]) {
            NSLog(@"for in 的倒序:%@",string);
        }
        // 可变字符串
        NSMutableString *newStr = [NSMutableString string];
        for (NSString *str in stringArr) {
            [newStr appendString:str];
            
        }
        NSLog(@"%@",newStr);
        // 不可变字符串
        NSString *newStr1 = [NSString string];
        for (NSString *str in stringArr) {
            newStr1 = [newStr1 stringByAppendingString:str];
        }
        NSLog(@"%@",newStr1);
        // 快速枚举一个字典
        for (NSString *key in dic) {
            NSLog(@"key :%@",key);
            NSLog(@"value:%@",[dic objectForKey:key]);
        }
        // 快速枚举一个集合
        for (NSString *anyObject in set) {
            NSLog(@"%@",anyObject);
        }
        // 排序1
        // 参数1 key:排序条件
        // 参数2 acending: YES(升序) Or NO(降序)
        NSArray *arr = @[@"1", @"2", @"3", @"4"];
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
        // 数组有排序的方法(sortedArrayUsingDescriptors)
        // 参数为一个排序条件的数组
        arr = [arr sortedArrayUsingDescriptors:@[sortDescriptor]];
        NSLog(@"%@",arr);
        
        
        NSMutableArray *mutableArr = @[@"1", @"2", @"3", @"4"].mutableCopy;
        // 数组有排序的方法(sortedArrayUsingDescriptors)
        // 参数为一个排序条件的数组
        [mutableArr sortUsingDescriptors:@[sortDescriptor]];
        NSLog(@"%@",mutableArr);
        // 排序: 可变数组和不可变数组,通过sort排序调用的方法不同
        // 1.不可变数组排序后需要生成新的数组
        // 2.可变数组排序直接可以在本身修改
        Person *per = [[Person alloc] init];
        per.name = @"启明男";
        per.age = 100;
        Person *per1 = [[Person alloc] init];
        per1.name = @"启明男1";
        per1.age = 200;
        Person *per2 = [[Person alloc] init];
        per2.name = @"启明男2";
        per2.age = 300;
        NSArray *perArray = @[per,per1,per2];
        // 根据名字name 排序
        NSSortDescriptor *perSort = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
        perArray = [perArray sortedArrayUsingDescriptors:@[perSort]];
        NSLog(@"%@",perArray);
        // 排序2 sortedArrayUsingSelector
        NSArray *sortString = @[@"A", @"J",@"C", @"D", @"E"];
        sortString = [sortString sortedArrayUsingSelector:@selector(compare:)];
        NSLog(@"%@",sortString);
    }
    return 0;
}
